<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户姓名" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户手机号" prop="customerPhone">
        <el-input
          v-model="queryParams.customerPhone"
          placeholder="请输入客户手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户年龄段" prop="customerAge">
        <el-input
          v-model="queryParams.customerAge"
          placeholder="请输入客户年龄段"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户职业/职务" prop="customerWord">
        <el-input
          v-model="queryParams.customerWord"
          placeholder="请输入客户职业/职务"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户风格喜好" prop="customerStyle">
        <el-input
          v-model="queryParams.customerStyle"
          placeholder="请输入客户风格喜好"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户来源" prop="customerSource">
        <el-input
          v-model="queryParams.customerSource"
          placeholder="请输入客户来源"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营销标签ID" prop="saleTagId">
        <el-input
          v-model="queryParams.saleTagId"
          placeholder="请输入营销标签ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营销标签名称" prop="saleTagName">
        <el-input
          v-model="queryParams.saleTagName"
          placeholder="请输入营销标签名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="面积标签ID" prop="areaTagId">
        <el-input
          v-model="queryParams.areaTagId"
          placeholder="请输入面积标签ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="面积标签名称" prop="areaTagName">
        <el-input
          v-model="queryParams.areaTagName"
          placeholder="请输入面积标签名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户装修面积" prop="customerArea">
        <el-input
          v-model="queryParams.customerArea"
          placeholder="请输入客户装修面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户装修区域" prop="customerRegion">
        <el-input
          v-model="queryParams.customerRegion"
          placeholder="请输入客户装修区域"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="录入人员id" prop="inputId">
        <el-input
          v-model="queryParams.inputId"
          placeholder="请输入录入人员id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="派单员id" prop="distributorId">
        <el-input
          v-model="queryParams.distributorId"
          placeholder="请输入派单员id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设计师id" prop="designerId">
        <el-input
          v-model="queryParams.designerId"
          placeholder="请输入设计师id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设计师拒绝次数" prop="refuseNum">
        <el-input
          v-model="queryParams.refuseNum"
          placeholder="请输入设计师拒绝次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="随机派单时设计师ids
用,分割" prop="candidateIds">
        <el-input
          v-model="queryParams.candidateIds"
          placeholder="请输入随机派单时设计师ids
用,分割"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="录入人名称" prop="inputName">
        <el-input
          v-model="queryParams.inputName"
          placeholder="请输入录入人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="派单员名称" prop="distributorName">
        <el-input
          v-model="queryParams.distributorName"
          placeholder="请输入派单员名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设计师名称" prop="designerName">
        <el-input
          v-model="queryParams.designerName"
          placeholder="请输入设计师名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="派单时间" prop="distributeTime">
        <el-date-picker clearable
          v-model="queryParams.distributeTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择派单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最后接受时间" prop="acceptTime">
        <el-date-picker clearable
          v-model="queryParams.acceptTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最后接受时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最后拒绝时间" prop="refuseTime">
        <el-date-picker clearable
          v-model="queryParams.refuseTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最后拒绝时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最后状态变更时间" prop="statusChangeTime">
        <el-date-picker clearable
          v-model="queryParams.statusChangeTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最后状态变更时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['biz:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['biz:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="客户姓名" align="center" prop="customerName" />
      <el-table-column label="客户手机号" align="center" prop="customerPhone" />
      <el-table-column label="客户姓名0女1男-1未知" align="center" prop="customerSex" />
      <el-table-column label="客户年龄段" align="center" prop="customerAge" />
      <el-table-column label="客户职业/职务" align="center" prop="customerWord" />
      <el-table-column label="客户风格喜好" align="center" prop="customerStyle" />
      <el-table-column label="客户装修经历" align="center" prop="customerExperience" />
      <el-table-column label="客户关注点" align="center" prop="customerAttention" />
      <el-table-column label="客户来源" align="center" prop="customerSource" />
      <el-table-column label="客户类型" align="center" prop="customerType" />
      <el-table-column label="营销标签ID" align="center" prop="saleTagId" />
      <el-table-column label="营销标签名称" align="center" prop="saleTagName" />
      <el-table-column label="面积标签ID" align="center" prop="areaTagId" />
      <el-table-column label="面积标签名称" align="center" prop="areaTagName" />
      <el-table-column label="客户装修面积" align="center" prop="customerArea" />
      <el-table-column label="客户装修区域" align="center" prop="customerRegion" />
      <el-table-column label="客户地址" align="center" prop="customerAddress" />
      <el-table-column label="录入人员id" align="center" prop="inputId" />
      <el-table-column label="派单员id" align="center" prop="distributorId" />
      <el-table-column label="设计师id" align="center" prop="designerId" />
      <el-table-column label="流程状态" align="center" prop="processStatus" />
      <el-table-column label="订单状态 0=未知1=有效2=无效" align="center" prop="orderStatus" />
      <el-table-column label="设计师拒绝次数" align="center" prop="refuseNum" />
      <el-table-column label="派单类型" align="center" prop="distributeType" />
      <el-table-column label="随机派单时设计师id" align="center" prop="candidateIds" />
      <el-table-column label="录入人名称" align="center" prop="inputName" />
      <el-table-column label="派单员名称" align="center" prop="distributorName" />
      <el-table-column label="设计师名称" align="center" prop="designerName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="派单时间" align="center" prop="distributeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.distributeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后接受时间" align="center" prop="acceptTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.acceptTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后拒绝时间" align="center" prop="refuseTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.refuseTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后状态变更时间" align="center" prop="statusChangeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.statusChangeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['biz:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户姓名" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户姓名" />
        </el-form-item>
        <el-form-item label="客户手机号" prop="customerPhone">
          <el-input v-model="form.customerPhone" placeholder="请输入客户手机号" />
        </el-form-item>
        <el-form-item label="客户年龄段" prop="customerAge">
          <el-input v-model="form.customerAge" placeholder="请输入客户年龄段" />
        </el-form-item>
        <el-form-item label="客户职业/职务" prop="customerWord">
          <el-input v-model="form.customerWord" placeholder="请输入客户职业/职务" />
        </el-form-item>
        <el-form-item label="客户风格喜好" prop="customerStyle">
          <el-input v-model="form.customerStyle" placeholder="请输入客户风格喜好" />
        </el-form-item>
        <el-form-item label="客户装修经历" prop="customerExperience">
          <el-input v-model="form.customerExperience" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="客户关注点" prop="customerAttention">
          <el-input v-model="form.customerAttention" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="客户来源" prop="customerSource">
          <el-input v-model="form.customerSource" placeholder="请输入客户来源" />
        </el-form-item>
        <el-form-item label="营销标签ID" prop="saleTagId">
          <el-input v-model="form.saleTagId" placeholder="请输入营销标签ID" />
        </el-form-item>
        <el-form-item label="营销标签名称" prop="saleTagName">
          <el-input v-model="form.saleTagName" placeholder="请输入营销标签名称" />
        </el-form-item>
        <el-form-item label="面积标签ID" prop="areaTagId">
          <el-input v-model="form.areaTagId" placeholder="请输入面积标签ID" />
        </el-form-item>
        <el-form-item label="面积标签名称" prop="areaTagName">
          <el-input v-model="form.areaTagName" placeholder="请输入面积标签名称" />
        </el-form-item>
        <el-form-item label="客户装修面积" prop="customerArea">
          <el-input v-model="form.customerArea" placeholder="请输入客户装修面积" />
        </el-form-item>
        <el-form-item label="客户装修区域" prop="customerRegion">
          <el-input v-model="form.customerRegion" placeholder="请输入客户装修区域" />
        </el-form-item>
        <el-form-item label="客户地址" prop="customerAddress">
          <el-input v-model="form.customerAddress" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="录入人员id" prop="inputId">
          <el-input v-model="form.inputId" placeholder="请输入录入人员id" />
        </el-form-item>
        <el-form-item label="派单员id" prop="distributorId">
          <el-input v-model="form.distributorId" placeholder="请输入派单员id" />
        </el-form-item>
        <el-form-item label="设计师id" prop="designerId">
          <el-input v-model="form.designerId" placeholder="请输入设计师id" />
        </el-form-item>
        <el-form-item label="设计师拒绝次数" prop="refuseNum">
          <el-input v-model="form.refuseNum" placeholder="请输入设计师拒绝次数" />
        </el-form-item>
        <el-form-item label="随机派单时设计师ids
用,分割" prop="candidateIds">
          <el-input v-model="form.candidateIds" placeholder="请输入随机派单时设计师ids
用,分割" />
        </el-form-item>
        <el-form-item label="录入人名称" prop="inputName">
          <el-input v-model="form.inputName" placeholder="请输入录入人名称" />
        </el-form-item>
        <el-form-item label="派单员名称" prop="distributorName">
          <el-input v-model="form.distributorName" placeholder="请输入派单员名称" />
        </el-form-item>
        <el-form-item label="设计师名称" prop="designerName">
          <el-input v-model="form.designerName" placeholder="请输入设计师名称" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="派单时间" prop="distributeTime">
          <el-date-picker clearable
            v-model="form.distributeTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择派单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后接受时间" prop="acceptTime">
          <el-date-picker clearable
            v-model="form.acceptTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后接受时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后拒绝时间" prop="refuseTime">
          <el-date-picker clearable
            v-model="form.refuseTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后拒绝时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后状态变更时间" prop="statusChangeTime">
          <el-date-picker clearable
            v-model="form.statusChangeTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后状态变更时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/biz/order";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: null,
        customerPhone: null,
        customerSex: null,
        customerAge: null,
        customerWord: null,
        customerStyle: null,
        customerExperience: null,
        customerAttention: null,
        customerSource: null,
        customerType: null,
        saleTagId: null,
        saleTagName: null,
        areaTagId: null,
        areaTagName: null,
        customerArea: null,
        customerRegion: null,
        customerAddress: null,
        inputId: null,
        distributorId: null,
        designerId: null,
        processStatus: null,
        orderStatus: null,
        refuseNum: null,
        distributeType: null,
        candidateIds: null,
        inputName: null,
        distributorName: null,
        designerName: null,
        distributeTime: null,
        acceptTime: null,
        refuseTime: null,
        statusChangeTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerName: [
          { required: true, message: "客户姓名不能为空", trigger: "blur" }
        ],
        customerPhone: [
          { required: true, message: "客户手机号不能为空", trigger: "blur" }
        ],
        customerSex: [
          { required: true, message: "客户姓名0女1男-1未知不能为空", trigger: "change" }
        ],
        customerArea: [
          { required: true, message: "客户装修面积不能为空", trigger: "blur" }
        ],
        inputId: [
          { required: true, message: "录入人员id不能为空", trigger: "blur" }
        ],
        processStatus: [
          { required: true, message: "流程状态不能为空", trigger: "change" }
        ],
        orderStatus: [
          { required: true, message: "订单状态 0=未知1=有效2=无效不能为空", trigger: "change" }
        ],
        refuseNum: [
          { required: true, message: "设计师拒绝次数不能为空", trigger: "blur" }
        ],
        inputName: [
          { required: true, message: "录入人名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerName: null,
        customerPhone: null,
        customerSex: null,
        customerAge: null,
        customerWord: null,
        customerStyle: null,
        customerExperience: null,
        customerAttention: null,
        customerSource: null,
        customerType: null,
        saleTagId: null,
        saleTagName: null,
        areaTagId: null,
        areaTagName: null,
        customerArea: null,
        customerRegion: null,
        customerAddress: null,
        inputId: null,
        distributorId: null,
        designerId: null,
        processStatus: null,
        orderStatus: null,
        refuseNum: null,
        distributeType: null,
        candidateIds: null,
        inputName: null,
        distributorName: null,
        designerName: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        distributeTime: null,
        acceptTime: null,
        refuseTime: null,
        statusChangeTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('biz/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
